package com.pearadmin.modules.sys.controller.manage;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.constant.ControllerConstant;
import com.pearadmin.common.tools.SecurityUtil;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.modules.sys.domain.InvAnode;
import com.pearadmin.modules.sys.domain.SysUser;
import com.pearadmin.modules.sys.service.IInvAnodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

/**
 * 阳极库存Controller
 *
 * @author Mr.Ling
 * @date 2021-11-26
 */
@RestController
@Api(tags = {"阳极库存管理"})
@RequestMapping(ControllerConstant.API_MANAGE_PREFIX + "invAnode")
public class InvAnodeController extends BaseController {
    private String prefix = "manage/invAnode" ;

    @Autowired
    private IInvAnodeService invAnodeService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/manage/invAnode/main','manage:invAnode:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询阳极库存列表
     */
    @ResponseBody
    @ApiOperation("列表")
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/manage/invAnode/data','manage:invAnode:data')")
    public ResultTable list(@ModelAttribute InvAnode invAnode, PageDomain pageDomain) {
        PageInfo<InvAnode> pageInfo = invAnodeService.selectInvAnodePage(invAnode, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增阳极库存
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/manage/invAnode/add','manage:invAnode:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存阳极库存
     */
    @ResponseBody
    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/manage/invAnode/add','manage:invAnode:add')")
    public Result save(@RequestBody InvAnode invAnode) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        invAnode.setCreateTime(LocalDateTime.now());
        invAnode.setCreateBy(sysUser.getUserId());
        invAnode.setCreateName(sysUser.getRealName());
        invAnode.setId(SequenceUtil.makeStringId());
        return decide(invAnodeService.insertInvAnode(invAnode));
    }

    /**
     * 修改阳极库存
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/manage/invAnode/edit','manage:invAnode:edit')")
    public ModelAndView edit(String id, ModelMap mmap) {
        InvAnode invAnode = invAnodeService.selectInvAnodeById(id);
        invAnode.setUpdateTime(LocalDateTime.now());
        mmap.put("invAnode", invAnode);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存阳极库存
     */
    @ResponseBody
    @ApiOperation("更新")
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/manage/invAnode/edit','manage:invAnode:edit')")
    public Result update(@RequestBody InvAnode invAnode) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        invAnode.setUpdateTime(LocalDateTime.now());
        invAnode.setUpdateBy(sysUser.getUserId());
        invAnode.setUpdateName(sysUser.getUsername());
        return decide(invAnodeService.updateInvAnode(invAnode));
    }

    /**
     * 删除阳极库存
     */
    @ResponseBody
    @ApiOperation("批量刪除")
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/manage/invAnode/remove','manage:invAnode:remove')")
    public Result batchRemove(String ids) {
        return decide(invAnodeService.deleteInvAnodeByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @ApiOperation("根据id 刪除")
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/manage/invAnode/remove','manage:invAnode:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(invAnodeService.deleteInvAnodeById(id));
    }
}
